package com.aizhixin.lab.project.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.course.domain.TaskDegreeOfCompletionDomain;
import com.aizhixin.lab.project.course.entity.TeacherProjectModule;
import com.aizhixin.lab.project.course.service.TeacherProjectModuleService;
import com.aizhixin.lab.project.template.data.ModuleData;
import com.aizhixin.lab.project.template.domain.ProjectModuleDomain;
import com.aizhixin.lab.project.template.entity.ProjectModule;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/v1/teacher/project/projectModule")
@RestController
@Api(tags = "模块",value = "模块")
public class TeacherModuleController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private TeacherProjectModuleService moduleService;
    @RequestMapping(value = "/updateSort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排序", httpMethod = "POST", notes = "修改排序<br><br><b>lwq</b>")
    public ResponseEntity<?> updateSort(@RequestHeader("Authorization") String token, @RequestBody List<String> ids) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        moduleService.updateSort(ids);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.MESSAGE, "修改成功");
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增模块", httpMethod = "POST", notes = "新增模块<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody ProjectModuleDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }try{
            TeacherProjectModule save = moduleService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save.getId());
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findModuleList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据项目Id获取该模块下的模块列表", httpMethod = "GET", notes = "根据项目Id获取该模块下的模块列表<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectModuleDomain.class),})
    public ResponseEntity<?> findModuleList(@RequestHeader("Authorization") String token, @RequestParam(name = "projectId") String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<ProjectModuleDomain> moduleByProject = moduleService.findModuleByProject(projectId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,moduleByProject);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findAllModuleDataList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询所有模块列表及任务工单列表", httpMethod = "GET", notes = "查询所有模块列表及任务工单列表<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ModuleData.class),})
    public ResponseEntity<?> findAllModuleDataList(@RequestHeader("Authorization") String token, @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<ModuleData> moduleByProject = moduleService.findAllModuleData(projectId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,moduleByProject);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findModuleInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取模块详情", httpMethod = "GET", notes = "获取模块详情<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectModuleDomain.class),})
    public ResponseEntity<?> findModuleInfo(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            ProjectModuleDomain module = moduleService.findModuleById(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,module);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除模块", httpMethod = "DELETE", notes = "删除模块<br><br><b>lwq</b>")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "模块ID", required = true)  @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            result=moduleService.deleteModule(id);

        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }



}
